package com.ys.tdm.dao.item.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.ys.tdm.bean.item.Item;
import com.ys.tdm.dao.item.IItemDao;
import com.ys.tdm.templ.MybatisTemplate;
import com.ys.tdm.util.StringTools;
import com.ys.tdm.util.UUIDGenerator;




/**
 * 模块信息Dao实现
 * @author Troy
 * @date 2017-12-27 15:48:30
 */
@Repository
public class ItemDaoImpl extends MybatisTemplate implements IItemDao {

	@Override
	public Item getItemById(String id) throws Exception {
		return (Item)this.selectOne("ItemXML.getItemById", id);
	}
	
	@Override
	public List<Item> getItemByIds(String ids) throws Exception {
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("ids", ids);
		return this.selectList("ItemXML.getItemByIds", params);
	}
	
	@Override
	public List<Item> getItemByIdsList(List<String> ids) throws Exception {
		return this.selectList("ItemXML.getItemByIdsList", ids);
	}

	@Override
	public List<Item> getAll(Item item) throws Exception {
		return this.selectList("ItemXML.getAll", item);
	}
	
	
	@Override
	public int getByPageCount(Item item)throws Exception {
		return this.selectOne("ItemXML.getByPageCount", item);
	}

	@Override
	public void insertItem(Item item) throws Exception {
		if (null != item) {
			item.setId(UUIDGenerator.generate());
			item.setCreateTime(new Date());
			item.setUpdateTime(new Date());
			this.insert("ItemXML.insertItem", item);
		}
	}
	
	@Override
	public void insertItemBatch(List<Item> items) throws Exception {
		if(CollectionUtils.isNotEmpty(items)){
			for (Item item : items) {
				if (null != item) {
					item.setId(UUIDGenerator.generate());
					item.setCreateTime(new Date());
				}
			}
			this.insert("ItemXML.insertItemBatch", items);
		}
	}
	
	@Override
	public void delItemById(String id) throws Exception {
		this.delete("ItemXML.delItemById", id);
	}
	
	@Override
	public void delItemByIds(String ids) throws Exception {
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("ids", ids);
		this.delete("ItemXML.delItemByIds", params);
	}
	
	@Override
	public void delItemByIdsList(List<String> ids) throws Exception {
		this.delete("ItemXML.delItemByIdsList", ids);
	}
	
	@Override
	public int updateItem(Item item) throws Exception {
		if (null != item) {
			item.setUpdateTime(new Date());
			/**清理不需要更新的数据*/
		    this.cleanWhenUpdate(item);
			return this.update("ItemXML.updateItem", item);
		} else {
			return 0;
		}
	}
	
	@Override
	public int updateItemByIds(String ids,Item item) throws Exception {
		ids = StringTools.converString(ids);
		if (StringUtils.isNotBlank(ids) && null != item) {
			item.setUpdateTime(new Date());
			Map<String,Object> params = new HashMap<String, Object>();
			params.put("ids", ids);
			/**清理不需要更新的数据*/
		    this.cleanWhenUpdate(item);
			params.put("item", item);
			return this.update("ItemXML.updateItemByIds", params);
		} else {
			return 0;
		}
		
	}
	
	@Override
	public int updateItemByIdsList(List<String> ids,Item item) throws Exception {
		if (CollectionUtils.isNotEmpty(ids) && null != item) {
			item.setUpdateTime(new Date());
			Map<String,Object> params = new HashMap<String, Object>();
			params.put("ids", ids);
			/**清理不需要更新的数据*/
		    this.cleanWhenUpdate(item);
			params.put("item", item);
			return this.update("ItemXML.updateItemByIdsList", params);
		} else {
			return 0;
		}
	}
	
	@Override
	public int updateItemList(List<Item> items) throws Exception {
		if(CollectionUtils.isNotEmpty(items)){
			for (Item item : items) {
				if (null != item) {
					item.setUpdateTime(new Date());
					/**清理不需要更新的数据*/
					this.cleanWhenUpdate(item);
				}
			}
			return this.update("ItemXML.updateItemList", items);
		} else {
			return 0;
		}
	}
	
	/**
	 * 清理不需要更新的数据
	 * @param orderRebok
	 * @Description:
	 * @author wentaoxiang 2016年6月1日 下午5:19:16
	 */
	private void cleanWhenUpdate(Item item) {
		item.setCreateTime(null);
		item.setCreator(null);
	}
	
	//------------api------------
}

